#lang racket
(require "ircbotbase.rkt")

(define port (make-parameter #f))
(verbose #t)



(define (valid-cmd-privmsg? m bot)
  (and (regexp-match (regexp (string-append
                              ":(.*)!(.*) PRIVMSG .* :!(.*)"))
                     m)
       (hash-ref admins (parse-name m) (λ () #f))
       (hash-ref actions (first (regexp-split #rx" " (parse-cmd m))) (λ() #f))))
(define (valid-privmsg? m bot)
  (regexp-match (regexp (string-append
                         ":(.*)!(.*) PRIVMSG .* :(.*)"))
                m))

(define (echo msg bot)
  (mesg (string-append (parse-name msg) ": oh i i hear you") bot)
  (mesg (string-append "you said: " (last (regexp-split #rx":" msg))) bot))

(define (cmd-matcher msg bot)
  (define cmd (parse-cmd msg))
  (do-cmd cmd bot))
  
(define (parse-name msg)
  (first (regexp-split #rx"!" (second (regexp-split #rx":" msg)))))

(define (parse-cmd msg)
  (last (regexp-split #rx":" msg)))

(define (do-cmd cmd bot)
  (define c+args (regexp-split #rx" " cmd))
  (define action (hash-ref actions (first c+args)))
  (action bot (rest c+args)))

(define (hybernate bot args)
  (cond [(empty? args)
         (mesg "all hybernate" bot)]
        [else (for ([n args])
                (mesg (string-append n ": hybernate") bot))]))

(define (sleep bot args)
  (cond [(empty? args)
         (mesg "all sleep" bot)]
        [else (for ([n args])
                (mesg (string-append n ": sleep")))]))  

(define admins
  #hash(("florence" . #t)
        ("hyfi" . #t)
        ("caminyx" . #t)))

(define actions
  (make-hash (list (cons "!hibernate" hybernate)
                   (cons "!sleep"  sleep))))

#;(define admbot 
  (command-line
   #:once-each
   [("-v" "--verbose") "shows activity"
                       (verbose #t)]
   [("-p" "--port") p "port for server"
                    (port p)]
   #:args (serv chann nick)
   (if (port)
       (make-ircbot serv chann nick #:port (port))
       (make-ircbot serv chann nick))))

(define admbot (make-ircbot "tv" "#testing" "admbot"))
(define echoer (make-ircbot "tv" "#testing" "echobot"))

(add-lookup! admbot valid-cmd-privmsg? cmd-matcher)
(add-lookup! echoer valid-privmsg? echo)




